package com.crm.service.before_sale.impl;

import com.crm.dao.before_sale.NeedDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.ClientNeed;
import com.crm.pojo.SalePlan;
import com.crm.service.before_sale.ClientNeedService;
import com.crm.vo.ClientNeedVO;
import com.crm.vo.Result;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClientNeedServiceimpl implements ClientNeedService {
    @Autowired
    private NeedDAO needDAO;


    /**
     * 分页展示客户需求
     * @param clientNeed
     * @param curpage
     * @param pagesize
     * @param recodeStartTime
     * @param recodeEndTime
     * @return
     */
    @Override
    public List<ClientNeed> showClientNeedList(ClientNeed clientNeed, int curpage, int pagesize, Date recodeStartTime, Date recodeEndTime) {
        Assert.notNull(clientNeed);
        PageRequest pageRequest = new PageRequest(curpage-1,pagesize, Sort.Direction.DESC,"needId");
        @SuppressWarnings("unchecked")
        Page<ClientNeed> page = needDAO.findAll(new Specification<ClientNeed>() {
            @Override
            public Predicate toPredicate(Root<ClientNeed> root, CriteriaQuery<?> argl, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //根据以下条件查询
                //1.需求主题
                if(!StringUtils.isEmpty(clientNeed.getNeedTheme())){
                    list.add(cb.like(root.get("needTheme").as(String.class),"%" + clientNeed.getNeedTheme() + "%"));
                }
                //2.重要程度
                if(!StringUtils.isEmpty(clientNeed.getImportance())){
                    list.add(cb.like(root.get("importance").as(String.class),"%" + clientNeed.getImportance() + "%"));

                }

                //没有被删除
                list.add(cb.notLike(root.get("clientNeedIsDel").as(String.class), "是"));

                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageRequest);

        List<ClientNeed> list = page.getContent();
        return list;
    }

    /**
     * 获取客户需求总数量
     * @param clientNeed
     * @param recodeStartTime
     * @param recodeEndTime
     * @return
     */
    @Override
    public Long getClientNeedCount(ClientNeed clientNeed, Date recodeStartTime, Date recodeEndTime) {
        Assert.notNull(clientNeed);
        Long toatal = needDAO.count(new Specification<ClientNeed>() {
            @Override
            public Predicate toPredicate(Root<ClientNeed> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //根据以下条件查询
                //1.需求主题
                if(!StringUtils.isEmpty(clientNeed.getNeedTheme())){
                    list.add(cb.like(root.get("needTheme").as(String.class),"%" + clientNeed.getNeedTheme() + "%"));
                }
                //2.重要程度
                if(!StringUtils.isEmpty(clientNeed.getImportance())){
                    list.add(cb.like(root.get("importance").as(String.class),"%" + clientNeed.getImportance() + "%"));

                }

                //没有被删除
                list.add(cb.notLike(root.get("clientNeedIsDel").as(String.class), "是"));

                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        });

        return toatal;
    }

    /**
     * 删除客户需求
     * @param needId
     * @return
     */
    @Override
    public Result deleteClientNeed(Long needId) throws ServiceException{

        try {
            needDAO.deleteClientNeedById(needId);
            return Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);

        }
    }

    /**
     * 添加一个客户需求
     * @param needTheme
     * @param clientId
     * @param contactId
     * @param chanceId
     * @param recordTime
     * @param importance
     * @param needContent
     * @return
     */
    @Override
    public Result addClientNeed(String needTheme, BigDecimal clientId, BigDecimal contactId, BigDecimal chanceId, Date recordTime, String importance, String needContent) {
        try {
            ClientNeed clientNeed = new ClientNeed();
            clientNeed.setNeedTheme(needTheme);
            clientNeed.setClientId(clientId);
            clientNeed.setContactId(contactId);
            clientNeed.setChanceId(chanceId);
            clientNeed.setRecordTime(new Date());
            clientNeed.setImportance(importance);
            clientNeed.setNeedContent(needContent);
            clientNeed.setClientNeedIsDel("否");
            needDAO.save(clientNeed);
            return Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("Sorry,添加失败!");

        }
    }

    /**
     * 编辑
     * @param clientNeed
     * @return
     */
    @Override
    public Result editClientNeed(ClientNeed clientNeed) {
        try {

            clientNeed.setRecordTime(new Date());
            needDAO.save(clientNeed);
            return Result.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("Sorry,修改失败！");
        }
    }
}
